//
//  WBVisitorView.swift
//  Weibo
//
//  Created by Albert Zhu on 16/9/30.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

/// 访客视图
class WBVisitorView: UIView {
    
    /// 访客视图的信息字典
    var visitorInfo:[String:String]? {
        
        didSet {
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    return
            }
            
            tipLabel.text = message
            if imageName == "" {
                rotate()
                return
            }
            
            houseIconView.isHidden = true
            maskIconView.isHidden = true
            iconView.image = UIImage(named: imageName)
        }
    }
    /// 转轮
    lazy var iconView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    /// 转轮的遮罩
    lazy var maskIconView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    /// 小房子
    lazy var houseIconView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    /// 提示标签
    lazy var tipLabel = UILabel.az_label(text: "关注一些人，回这里看看有什么惊喜", textColor: UIColor.darkGray)
    /// 注册按钮
    lazy var registerBtn = UIButton.az_textButton(text: "注册", fontSize: 14, normalColor: UIColor.orange, highlightedColor: UIColor.darkGray, backgroundImage: #imageLiteral(resourceName: "common_button_white_disable"))
    /// 登录按钮
    lazy var loginBtn = UIButton.az_textButton(text: "登录", fontSize: 14, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange, backgroundImage: #imageLiteral(resourceName: "common_button_white_disable"))
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


// MARK: - 设置界面
extension WBVisitorView {
    
    func setupUI() {
        
        backgroundColor = UIColor.az_RGBColor(red: 237, green: 237, blue: 237)
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        // 取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 自动布局
        // 圆环
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -50))
        // 小房子
        addConstraint(NSLayoutConstraint(item: houseIconView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1.0, constant: 0))
        // 提示标签
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 236))
        tipLabel.numberOfLines = 0
        tipLabel.textAlignment = .center
        // 注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .left, relatedBy: .equal, toItem: tipLabel, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        // 登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .right, relatedBy: .equal, toItem: tipLabel, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        
        // 遮罩(VFL)
        let viewDic = ["maskIconView": maskIconView, "loginBtn": loginBtn] as [String: Any]
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: viewDic))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskIconView]-(-20)-[loginBtn]", options: [], metrics: nil, views: viewDic))
    }
    
    func rotate() {
        
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = M_PI * 2
        ani.repeatCount = MAXFLOAT
        ani.duration = 15
        ani.isRemovedOnCompletion = false
        
        iconView.layer.add(ani, forKey: nil)
    }
    
    @objc func ani() {
        UIView.animate(withDuration: 1/60) {
            self.iconView.transform = self.iconView.transform.rotated(by: CGFloat(M_PI)/CGFloat(1000))
        }
    }
}
